interface PostParams {
  login: {
    uid?: string;
    email?: string;
  };
  logout: {
    uid?: string;
    email?: string;
  };
  registerClick: {
    email?: string;
    referralCode?: string;
  };
  register: {
    uid?: string;
    email?: string;
    referralCode?: string;
  };
  rechargeClick: {
    isFiat?: number;
    amount?: string;
    currency: string;
  };
  recharge: {
    isFiat?: number;
    amount: string;
    currency: string;
  };
  firstrecharge: any;
  enterGame: {
    gameCode: string;
  };
}

export class JsBridge {
  public uid = '';
  public email = '';
  public cid = '';
  public domain = '';

  constructor() {
    this.domain = window.location.origin;
  }

  public post<T extends keyof PostParams>(name: T, params: PostParams[T]) {
    (window as any).jsBridge?.postMessage(name, JSON.stringify({
      uid: this.uid,
      email: this.email,
      cid: this.cid,
      domain: this.domain,
      ...params,
    }));
  }
}
